<?php
echo '<p>'.$this->message.'</p>';

$linkToGraph = '<a href="'.$this->url(array('controller' => 'graph', 'action' => 'index', 'graph' => $this->graph->getId())).'">'.$this->graph->getName().'</a>';
$linkToGraphs = '<a href="'.$this->url(array('controller' => 'graph', 'action' => 'index', 'graph' => '')).'">Grafi</a>';

echo '<h1>Scenārijs</h1>';
echo '<h2>'.$linkToGraphs.' '.$linkToGraph.'</h2>';

if(is_array($this->links) AND count($this->links) > 0){

    // graph navigation
    $currentLink = new My_Link($this->links[0]);
    $link = new My_Link();
    $upLinksResult = $link->readByEnd($currentLink->getStart());
    if(is_array($upLinksResult)){
        $upLink = new My_Link($upLinksResult[0]);
        $upLink->getStart();
        if($upLink->exists()){
            $linktUp = '<a href="'.$this->url(array('controller' => 'scenario', 'action' => 'index', 'id' => $upLink->getStart())).'">../</a> '.$currentLink->getStart();
        }else{
            $linktUp = $currentLink->getStart();
        }
    }
    

    echo $linktUp;
    echo '<table style="width:100%;">
            <tr>
                <th>Virsotnes id</th>
                <th>Objekts</th>
                <th>Saites svars</th>
                <th>Info</th>
                <th>Opcijas</th>
            </tr>';

    foreach($this->links AS $linkData){
        unset($link);
        unset($wertex);
        unset($sub);

        $link = new My_Link($linkData);
        $wertex = new My_Wertex($link->getEnd());
        $object = new My_Object($wertex->getObject());

        $plugins = $object->getPluginClassNames();
        if(!empty($plugins[0])){
            Zend_Loader::loadClass('Plugin_'.$plugins[0].'_Class');
            eval('$plugin = new Plugin_'.$plugins[0].'_Class('.$wertex->getId().');');
            $pluginContent = $plugin->getContent();
        }

        $linkSub = new My_Link();
        $return = $linkSub->readByStart($wertex->getId());
        if(is_array($return)){
            foreach($return AS $linkSub){
                $subLink = new My_Link($linkSub);
                $sub[] = $subLink->getEnd();
            }
        }
        
        if(count($sub)){
            $linkToNext = '<a href="'.$this->url(array('controller' => 'scenario', 'action' => 'index', 'id' => $wertex->getId())).'">'.$wertex->getId().'</a>';
        }else{
            $linkToNext = $wertex->getId();
        }

        echo '<tr>';
        echo '<td>'.$linkToNext.'</td>';
        echo '<td>'.$object->getName().'</td>';
        echo '<td>'.$link->getWeight().'</td>';
        echo '<td>'.$pluginContent.'</td>';
        echo '<td>';
            echo '<div style="float:left;">
                 <form method="POST" action="'.$this->url(array('controller' => 'scenario', 'action' => 'add', 'id' => $wertex->getId())).'">
                 <input type="submit" name="__submit" value="Jauns Zars" />
                 </form></div>';
            echo '<div style="float:left;">
                 <form method="POST" action="'.$this->url(array('controller' => 'scenario', 'action' => 'delete', 'id' => $wertex->getId())).'">
                 <input type="submit" name="__submit" value="Dzēst" />
                 </form></div>';
            echo '<div style="float:left;">
                 <form method="POST" action="'.$this->url(array('controller' => 'module', 'action' => 'edit', 'id' => $wertex->getId())).'">
                 <input type="submit" value="Rediģēt" />
                 </form></div>';
            echo '<div style="float:left;">
                 <form method="POST" action="'.$this->url(array('controller' => 'scenario', 'action' => 'weight', 'value' => '1', 'id' => $link->getId())).'">
                 <input type="hidden" name="graph" value="'.$this->graph->getId().'" />
                 <input type="submit" value="Up" />
                 </form></div>';
            echo '<div style="float:left;">
                 <form method="POST" action="'.$this->url(array('controller' => 'scenario', 'action' => 'weight', 'value' => '-1', 'id' => $link->getId())).'">
                 <input type="hidden" name="graph" value="'.$this->graph->getId().'" />
                 <input type="submit" value="Down" />
                 </form></div>';
        echo '</td>';
        echo '</tr>';
    }

    echo '</table>';
}

echo '<div style="float:left;">
         <form method="POST" action="'.
        $this->url(
            array(
                'controller' => 'scenario',
                'action' => 'add',
                'id' => $currentLink->getStart(),
            )).'">
         <input type="submit" name="__submit" value="Jauns" />
         </form></div>';

